package com.three.box.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.ViewGroup
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import com.hjq.toast.Toaster
import com.three.box.R
import com.three.box.base.BaseActivity
import com.three.box.databinding.ActivityAddProductOldBinding
import com.three.box.entity.ProductConfig
import com.three.box.network.AppRepository
import com.three.box.network.NetworkUtil

class AddProductActivityOld : BaseActivity() {

    companion object {
        const val TAG = "AddProductActivity"
    }

    private lateinit var binding: ActivityAddProductOldBinding

    private lateinit var adapter: ProductAdapter
    private val productConfigList = mutableListOf<ProductConfig>() // 列表当前数据
    private val checkedList = ArrayList<ProductConfig>() // 当前选中数据
    private lateinit var existProductIds: ArrayList<Int>

    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityAddProductOldBinding.inflate(layoutInflater)
        setStatusBar()
        super.onCreate(savedInstanceState)

        existProductIds = intent.getIntegerArrayListExtra("product_ids") ?: ArrayList()
        initView()

        initValues()
    }

    override fun getLayoutView() = binding.root

    private fun initValues() {
        productConfigList.addAll(AppRepository.instance.productConfigList)
        adapter.submitList(productConfigList)
    }

    private fun initView() {
        binding.backIv.setOnClickListener { finish() }
        binding.syncTv.setOnClickListener {
            if (!NetworkUtil.isNetworkAvailable(this)) {
                Toaster.show("网络错误，请检查后重试")
                return@setOnClickListener
            }
            AppRepository.instance.configInfo { code, msg ->
                if (code < 0) {
                    Toaster.show(msg)
                    return@configInfo
                }
                initValues()
            }
        }
        binding.confirmBtn.setOnClickListener {
            setResult(RESULT_OK, Intent().apply {
                putParcelableArrayListExtra("product_list", checkedList)
            })
            finish()
        }

        adapter = ProductAdapter()
        adapter.setOnItemClickListener { adapter, _, position ->
            val item = adapter.getItem(position) as ProductConfig
//            resultLaunch.launch(InspectionResultActivity.parseIntent(this, item.id, item.deviceType))
        }
        adapter.addOnItemChildClickListener(R.id.state_cb) { adapter, _, position ->
            val item = adapter.getItem(position) as ProductConfig
            if (checkedList.contains(item)) {
                checkedList.remove(item)
            } else {
                checkedList.add(item)
            }
            adapter.notifyItemChanged(position)
        }
        adapter.isStateViewEnable = true
        adapter.setStateViewLayout(this, R.layout.empty_product_config)

        with(binding) {
            recyclerView.layoutManager = LinearLayoutManager(this@AddProductActivityOld)
            recyclerView.adapter = adapter
            recyclerView.addItemDecoration(
                DividerItemDecoration(
                    this@AddProductActivityOld,
                    DividerItemDecoration.VERTICAL
                )
            )
        }
    }

    inner class ProductAdapter : BaseQuickAdapter<ProductConfig, QuickViewHolder>() {
        override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
            // 返回一个 ViewHolder
            return QuickViewHolder(R.layout.item_add_product, parent)
        }

        override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: ProductConfig?) {
            holder.setText(R.id.name_tv, item?.desc)
//            val model = if (item?.currentModel == null) item?.productModels?.get(0) else item.currentModel
//            holder.setText(R.id.model_tv, model?.name)
//            holder.getView<CheckBox>(R.id.state_cb).isChecked = checkedList.contains(item)
//            val modelLayout = holder.getView<LinearLayoutCompat>(R.id.model_layout)
//            modelLayout.setOnClickListener {
//                if (item == null) {
//                    Toaster.show("数据错误，请检查后重试")
//                    return@setOnClickListener
//                }
//                val modelDialog = ModelDialogFragment.newInstance(item)
//                modelDialog.setCallback { productModel ->
//                    item.currentModel = productModel
//                    adapter.notifyItemChanged(position)
//                }
//                modelDialog.show(supportFragmentManager, TAG)
//            }
        }
    }
}